#include "stdafx.h"
#include "object/GameObject.h"
#include "Transform.h"

using namespace unigame;

Transform::~Transform()
{
}

void Transform::makeLocalMatrix()
{
	if(getFlag(E_POSITION_DIRTY_TFF | E_OTHER_DIRTY_TFF))
	{
		if(getFlag(E_OTHER_DIRTY_TFF))
		{
			m_rotation.toMatrix(m_locale);
			m_locale.multScale(m_scale);
		}
		m_locale.setPosition(m_position);
		clearFlag(E_POSITION_DIRTY_TFF | E_OTHER_DIRTY_TFF);
		setFlag(E_LOCALE_WORLD_DIRTY_TFF);
	}
}

void Transform::onAddToFather()
{
	if(m_gameObject->father() == 0)
	{
		return;
	}
	setFlag(E_LOCALE_WORLD_DIRTY_TFF);
}

void Transform::onRemoveFromFather(GameObject* father)
{
	if(father == 0)
	{
		return;
	}
	setFlag(E_LOCALE_WORLD_DIRTY_TFF);
}

void Transform::update()
{
	makeLocalMatrix();
	if(getFlag(E_LOCALE_WORLD_DIRTY_TFF) 
		|| (m_gameObject->father() && m_gameObject->father()->getFlag(E_TRANSFORM_CHANGE_GOF)))
	{
		makeLocaleToWorld();
	}
}

void Transform::makeLocaleToWorld()
{
	m_isWorldLocaleDirty = true;
	setFlag(E_LOCALE_WORLD_DIRTY_TFF);
	if(m_gameObject == 0 || m_gameObject->father() == 0)
	{
		m_localeToWorld = m_locale;
		return;
	}
	m_localeToWorld = m_gameObject->father()->transform().m_localeToWorld * m_locale;
}